package com.pwx.services.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.pwx.dao.EmpMapper;
import com.pwx.entity.Emp;
import com.pwx.services.EmpServices;
import com.pwx.utils.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * Copyright (C),2021-2022,亚瑟王
 * FileName: EmpServicesImpl
 * Author: saber
 * Date:2022/9/29 18:56
 */
@Service
@Transactional
public class EmpServicesImpl implements EmpServices {

    @Autowired
    private EmpMapper em;

    @Override
    @Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
    public Emp doLoging(String userName, String password) {
        Emp emp = em.getByEname(userName);
        if (emp!=null&&emp.getPwd().equals(password)) {
            return emp;
        }
        return null;
    }


    @Override
    @Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
    public PageInfo<Emp> getAll(Map<String, Object> maps) {
        int pageNum = StringUtil.stringToInt((String) maps.get("pageNum"));
        PageHelper.startPage(pageNum,3);
        List<Emp> list = em.getAll(maps);
        PageInfo<Emp> info = new PageInfo<>(list);
        return info;
    }

    @Override
    public boolean addToEmp(Emp emp) {
        if (emp!=null){
            return em.addByEmp(emp);
        }else{
            return false;
        }
    }



    @Override
    public boolean deleteEmpByEno(Integer eno) {
        if (eno>0){
            return em.deleteByEno(eno);
        }else{
            return false;
        }
    }

    @Override
    @Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
    public Emp getEmpByEno(Integer eno) {
        if (eno>0){
            return em.findByEno(eno);
        }else{
            return null;
        }
    }

    @Override
    public boolean updateEmp(Emp emp) {
        if (emp!=null){
            return em.upadteByEmp(emp);
        }else{
            return false;
        }
    }
}
